/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.frame.service;

import java.util.Enumeration;
import java.util.Vector;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.widget.Desktop;
import org.kalmeo.kuix.widget.PopupBox;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.kuix.widget.TextField;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.util.frame.Frame;
import org.kalmeo.util.frame.FrameHandler;
import org.mobiubiq.directory.client.data.Service;
import org.mobiubiq.directory.client.data.ServiceSettingsRecordStore;
import org.mobiubiq.directory.client.data.Setting;
import org.mobiubiq.directory.client.frame.AbstractBaseFrame;

/**
 *
 * @author Marnix Renne
 */
public class ServiceSettingsFrame implements Frame {

    private Service service;
    private Screen screen;

    public ServiceSettingsFrame(Service service) {
        this.service = service;
        screen = Kuix.loadScreen("service-settings.xml", null);
        screen.setCleanUpWhenRemoved(true);
        System.out.println("screen loaded");
    }

    public boolean onMessage(Object identifier, Object[] arguments) {
        if ("save".equals(identifier)) {
            try {
                Desktop desktop = Kuix.getCanvas().getDesktop();
                Enumeration settings = service.getSettings().elements();
                while (settings.hasMoreElements()) {
                    Setting setting = (Setting) settings.nextElement();
                    Text text = (Text) desktop.getWidget(setting.getName());
                    setting.setValue(text.getText());
                }
                ServiceSettingsRecordStore.setSettings(service);
                FrameHandler frameHandler = Kuix.getFrameHandler();
                PopupBox popup = Kuix.alert(Kuix.getMessage("SETTINGS_SAVED"), KuixConstants.ALERT_INFO);
                popup.requestFocus();
                Frame frame = ServiceFrameFactory.getServiceFrame(service);
                frameHandler.pushFrame(frame);
                frameHandler.removeFrame(this);
                return false;
            } catch (Throwable t) {
                Kuix.alert(t);
            }
        }
        if ("cancel".equals(identifier)) {
            FrameHandler frameHandler = Kuix.getFrameHandler();
            frameHandler.removeFrame(this);
            AbstractBaseFrame topFrame = (AbstractBaseFrame) frameHandler.getTopFrame();
            topFrame.showScreen();
            return false;
        }
        return true;
    }

    public void onAdded() {
        screen.setCurrent();
        Vector settings = service.getSettings();
        Enumeration e = settings.elements();
        Text serviceName = (Text) Kuix.getCanvas().getDesktop().getWidget("serviceName");
        serviceName.setText(service.getName());
        Desktop desktop = Kuix.getCanvas().getDesktop();
        //Picture icon = (Picture) desktop.getWidget("serviceIcon");
        //icon.setImage(service.getIcon());
        
        Widget widget = desktop.getWidget("container");
        while (e.hasMoreElements()) {
            Setting setting = (Setting) e.nextElement();
            Text text = new Text();
            text.setAttribute("style", "align: left;");
            text.setText(setting.getDisplayName());
            widget.add(text);

            TextField field = new TextField();
            field.setAttribute(KuixConstants.CONSTRAINTS_ATTRIBUTE, TextField.ANY + "," + TextField.NON_PREDICTIVE);
            field.setId(setting.getName());
            widget.add(field);
        }
    }

    public void onRemoved() {
        screen.cleanUp();
        screen = null;
        service = null;
    }
}
